import Mock from 'mockjs'
import apiUrl from '../src/api/url'

const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
}

const mockData = Mock.mock({
  'userGetPage|77': [
    {
      id: '@id',
      title: '@sentence(10, 20)',
      'status|1': ['published', 'draft', 'deleted'],
      author: 'name',
      display_time: '@datetime',
      pageviews: '@integer(300, 5000)',

      code: '@id',
      createdBy: '@id',
      createdDate: '@date',
      updateBy: '@id',
      updateDate: '@date',
      state: 1,
      remark: '@cword',
      deptCode: '1',
      roleCode: '1',
      userName: '@first',
      realName: '@cname',
      mobile: '15564562',
      sex: 1,
      status: 'on',
      companyCode: null,
      roleName: '1',
      companyName: null
    }
  ]
})

const users = {
  'admin-token': {
    roles: ['admin'],
    avatar:
      'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin',

    code: '001',
    companyCode: null,
    createdBy: '001',
    createdDate: '2020-04-08 09:36:23',
    deptCode: '1',
    id: '001',
    mobile: '1',
    realName: 'admin',
    remark: '123',
    roleCode: '1',
    sex: 1,
    state: 1,
    status: 'on',
    updateBy: '001',
    updateDate: '2020-04-08 09:36:28',
    userName: 'admin'
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar:
      'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor'
  }
}
// http://wop.zzcsoft6.com
export default [
  // user login
  {
    url: apiUrl.login,
    type: 'post',
    response: config => {
      const { userName } = config.body
      const { token } = tokens[userName]

      // mock error
      if (!token) {
        return {
          retCode: '000001',
          retMsg: '用户名或密码错误.'
        }
      }

      return {
        retCode: '000000',
        retData: token
      }
    }
  },

  // get user info
  {
    url: apiUrl.userGet,
    type: 'get',
    response: config => {
      // const { token } = config.query
      const info = users['admin-token']

      // mock error
      if (!info) {
        return {
          retCode: '000403',
          retMsg: '登录失败，无法获取用户详细信息.'
        }
      }

      return {
        retCode: '000000',
        retData: info
      }
    }
  },
  // user logout
  {
    url: '/vue-admin-template/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: '000000',
        data: 'success'
      }
    }
  },
  // 用户列表
  {
    url: apiUrl.userGetPage,
    type: 'post',
    response: _ => {
      return {
        retCode: '000000',
        retMsg: 'success'
      }
    }
  }
]
